source("recodes.R")

library(ggplot2)
library(arm)

# figure 1
m1 <- lm(I(local_white - pctwhite15) ~ anger + anxiety, wave2)
m2 <- lm(I(local_black - pctblack15) ~ anger + anxiety, wave2)
m3 <- lm(I(local_asian - pctasian15) ~ anger + anxiety, wave2)
m4 <- lm(I(local_hispanic - pcthisp15) ~ anger + anxiety, wave2)
m5 <- lm(I(local_immigrant - pctimm15) ~ anger + anxiety, wave2)

m6 <- lm(I(national_white - 61.5) ~ anger + anxiety, wave2)
m7 <- lm(I(national_black - 12.3) ~ anger + anxiety, wave2)
m8 <- lm(I(national_asian - 5.3) ~ anger + anxiety, wave2)
m9 <- lm(I(national_hispanic - 17.6) ~ anger + anxiety, wave2)
m10 <- lm(I(national_immigrant - 13) ~ anger + anxiety, wave2)

coefs1 <- c(sum(coef(m1)[1:2]), sum(coef(m2)[1:2]), sum(coef(m3)[1:2]), sum(coef(m4)[1:2]), sum(coef(m5)[1:2]), sum(coef(m6)[1:2]), sum(coef(m7)[1:2]), sum(coef(m8)[1:2]), sum(coef(m9)[1:2]), sum(coef(m10)[1:2]))

coefs2 <- c(sum(coef(m1)[c(1,3)]), sum(coef(m2)[c(1,3)]), sum(coef(m3)[c(1,3)]), sum(coef(m4)[c(1,3)]), sum(coef(m5)[c(1,3)]), sum(coef(m6)[c(1,3)]), sum(coef(m7)[c(1,3)]), sum(coef(m8)[c(1,3)]), sum(coef(m9)[c(1,3)]), sum(coef(m10)[c(1,3)]))

coefs3 <- c(coef(m1)[1], coef(m2)[1], coef(m3)[1], coef(m4)[1], coef(m5)[1], coef(m6)[1], coef(m7)[1], coef(m8)[1], coef(m9)[1], coef(m10)[1])

coefs4 <- c(coef(m1)[1], coef(m2)[1], coef(m3)[1], coef(m4)[1], coef(m5)[1], coef(m6)[1], coef(m7)[1], coef(m8)[1], coef(m9)[1], coef(m10)[1])

ses1 <- c(se.coef(m1)[2], se.coef(m2)[2], se.coef(m3)[2], se.coef(m4)[2], se.coef(m5)[2], se.coef(m6)[2], se.coef(m7)[2], se.coef(m8)[2], se.coef(m9)[1], se.coef(m10)[1])

ses2 <- c(se.coef(m1)[3], se.coef(m2)[3], se.coef(m3)[3], se.coef(m4)[3], se.coef(m5)[3], se.coef(m6)[3], se.coef(m7)[3], se.coef(m8)[3], se.coef(m9)[3], se.coef(m10)[3])

df <- data.frame(coefs = c(coefs1, coefs2), ses = c(ses1,ses2), intercepts = c(coefs3, coefs4))
df$emotions <- rep(c("Anger", "Anxiety"), each = 10)
df$level <- rep(c("Local", "National"), each = 5, length.out = 20)
df$level <- factor(df$level)
df$groups <- factor(rep(c("White", "Black", "Asian", "Hispanic", "Immigrant"),  4))
df$groups <- factor(df$groups, c("White", "Black", "Asian", "Hispanic", "Immigrant"))

df <- df[rep(c(1,6,11,16), 5) + rep(0:4, each = 4),]
limits <- aes(ymax = coefs + ses*1.96, ymin= coefs - ses*1.96)
df$em_lev <- paste(df$emotions, " (", df$level, ")", sep = "")

df$em_lev <- factor(df$em_lev, levels = c("Anger (Local)", "Anxiety (Local)", "Anger (National)", "Anxiety (National)"))

row.names(df) <- 1:20

p <- ggplot(df, aes(x=groups, y=coefs, col = em_lev, shape = em_lev)) + geom_pointrange(limits, position=position_dodge(width=.5)) + theme_bw() + labs(x = "Outcomes", y = "Average Misperception", col = "Level", shape = "Emotions", title = "Emotions and Misperceptions") + scale_colour_manual(name = "Condition & Outcome", labels = c("Anger (Local)", "Anxiety (Local)", "Anger (National)", "Anxiety (National)"), values = c("black", "black", "gray", "gray")) + scale_shape_manual(name = "Condition & Outcome", labels = c("Anger (Local)", "Anxiety (Local)", "Anger (National)", "Anxiety (National)"), values = c(19, 17, 19, 17))

intercepts <- rep(c(0, 4, 8, 12, 16), each = 2) + c(1,2)
x1 <- rep(c(1, 2, 3, 4, 5), each = 2) + c(-.25, .25)
x2 <- rep(c(1, 2, 3, 4, 5), each = 2)

for (i in intercepts) {
  
  s <- which(i == intercepts)
  
  p <- p + geom_segment(x = x1[s], y = df$intercepts[i], 
                        xend = x2[s], yend = df$intercepts[i], col = "black")
  
}


p + theme_bw() + theme(plot.title = element_text(hjust = 0.5)) + theme(text = element_text(size=20))

# figure 2

m1 <- lm(I(abs(local_white - pctwhite15)) ~ anger + anxiety, wave2)
m2 <- lm(I(abs(local_black - pctblack15)) ~ anger + anxiety, wave2)
m3 <- lm(I(abs(local_asian - pctasian15)) ~ anger + anxiety, wave2)
m4 <- lm(I(abs(local_hispanic - pcthisp15)) ~ anger + anxiety, wave2)
m5 <- lm(I(abs(local_immigrant - pctimm15)) ~ anger + anxiety, wave2)
m6 <- lm(I(abs(national_white - 61.5)) ~ anger + anxiety, wave2)
m7 <- lm(I(abs(national_black - 12.3)) ~ anger + anxiety, wave2)
m8 <- lm(I(abs(national_asian - 5.3)) ~ anger + anxiety, wave2)
m9 <- lm(I(abs(national_hispanic - 17.6)) ~ anger + anxiety, wave2)
m10 <- lm(I(abs(national_immigrant - 13)) ~ anger + anxiety, wave2)

coefs1 <- c(sum(coef(m1)[1:2]), sum(coef(m2)[1:2]), sum(coef(m3)[1:2]), sum(coef(m4)[1:2]), sum(coef(m5)[1:2]), sum(coef(m6)[1:2]), sum(coef(m7)[1:2]), sum(coef(m8)[1:2]), sum(coef(m9)[1:2]), sum(coef(m10)[1:2]))

coefs2 <- c(sum(coef(m1)[c(1,3)]), sum(coef(m2)[c(1,3)]), sum(coef(m3)[c(1,3)]), sum(coef(m4)[c(1,3)]), sum(coef(m5)[c(1,3)]), sum(coef(m6)[c(1,3)]), sum(coef(m7)[c(1,3)]), sum(coef(m8)[c(1,3)]), sum(coef(m9)[c(1,3)]), sum(coef(m10)[c(1,3)]))

coefs3 <- c(coef(m1)[1], coef(m2)[1], coef(m3)[1], coef(m4)[1], coef(m5)[1], coef(m6)[1], coef(m7)[1], coef(m8)[1], coef(m9)[1], coef(m10)[1])

coefs4 <- c(coef(m1)[1], coef(m2)[1], coef(m3)[1], coef(m4)[1], coef(m5)[1], coef(m6)[1], coef(m7)[1], coef(m8)[1], coef(m9)[1], coef(m10)[1])

ses1 <- c(se.coef(m1)[2], se.coef(m2)[2], se.coef(m3)[2], se.coef(m4)[2], se.coef(m5)[2], se.coef(m6)[2], se.coef(m7)[2], se.coef(m8)[2], se.coef(m9)[1], se.coef(m10)[1])

ses2 <- c(se.coef(m1)[3], se.coef(m2)[3], se.coef(m3)[3], se.coef(m4)[3], se.coef(m5)[3], se.coef(m6)[3], se.coef(m7)[3], se.coef(m8)[3], se.coef(m9)[3], se.coef(m10)[3])

df <- data.frame(coefs = c(coefs1, coefs2), ses = c(ses1,ses2), intercepts = c(coefs3, coefs4))
df$emotions <- rep(c("Anger", "Anxiety"), each = 10)
df$level <- rep(c("Local", "National"), each = 5, length.out = 20)
df$level <- factor(df$level)
df$groups <- factor(rep(c("White", "Black", "Asian", "Hispanic", "Immigrant"),  4))
df$groups <- factor(df$groups, c("White", "Black", "Asian", "Hispanic", "Immigrant"))

df <- df[rep(c(1,6,11,16), 5) + rep(0:4, each = 4),]
limits <- aes(ymax = coefs + ses*1.96, ymin= coefs - ses*1.96)
df$em_lev <- paste(df$emotions, " (", df$level, ")", sep = "")

df$em_lev <- factor(df$em_lev, levels = c("Anger (Local)", "Anxiety (Local)", "Anger (National)", "Anxiety (National)"))

row.names(df) <- 1:20

p <- ggplot(df, aes(x=groups, y=coefs, col = em_lev, shape = em_lev)) + geom_pointrange(limits, position=position_dodge(width=.5)) + theme_bw() + labs(x = "Outcomes", y = "Absolute Misperception", col = "Level", shape = "Emotions", title = "Emotions and Misperceptions") + scale_colour_manual(name = "Condition & Outcome", labels = c("Anger (Local)", "Anxiety (Local)", "Anger (National)", "Anxiety (National)"), values = c("black", "black", "gray", "gray")) + scale_shape_manual(name = "Condition & Outcome", labels = c("Anger (Local)", "Anxiety (Local)", "Anger (National)", "Anxiety (National)"), values = c(19, 17, 19, 17))

intercepts <- rep(c(0, 4, 8, 12, 16), each = 2) + c(1,2)
x1 <- rep(c(1, 2, 3, 4, 5), each = 2) + c(-.25, .25)
x2 <- rep(c(1, 2, 3, 4, 5), each = 2)

for (i in intercepts) {
  
  s <- which(i == intercepts)
  
  p <- p + geom_segment(x = x1[s], y = df$intercepts[i], 
                        xend = x2[s], yend = df$intercepts[i], col = "black")
  
}


p + theme_bw() + theme(plot.title = element_text(hjust = 0.5)) + theme(text = element_text(size=20))


